package com.rfid.emergencywarehouse.presenter;

import com.rfid.base.util.LogUtils;
import com.rfid.emergencywarehouse.entity.KW;
import com.rfid.emergencywarehouse.entity.KWAsset;
import com.rfid.emergencywarehouse.entity.AssetOrder;
import com.rfid.emergencywarehouse.entity.WorkAre;
import com.rfid.emergencywarehouse.net.NetApi;
import com.rfid.emergencywarehouse.net.entity.AssetOrderRes;
import com.rfid.emergencywarehouse.net.entity.GetAssetRes;
import com.rfid.emergencywarehouse.net.entity.KFKWRes;
import com.rfid.emergencywarehouse.net.entity.WorkAreRes;
import com.rfid.emergencywarehouse.view.IGetAssetView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GetAssetPresenter<T extends IGetAssetView> extends ReadTagPresenter<T>{


    public GetAssetPresenter() {
        setDisableShelf(true);
    }
    public void doGetAsset(String orderId,List<String> barcodes){
        mView.onShowLoading("正在领取物资");
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                String msg = "";
                boolean success = false;
                try {
                    GetAssetRes res = NetApi.INSTANCE.doGetAsset(orderId,barcodes);
                    if (res.getCode() == 0) {
                        success = true;
                    } else {
                        msg = res.getMsg();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    msg = e.getMessage();
                }
                boolean finalSuccess = success;
                String finalMsg = msg;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mView != null) {
                            mView.onHideLoading();
                            mView.onDoGetAssetResult(finalSuccess, finalMsg);
                        }
                    }
                });
            }
        });
    }

    public void getAssetOrderList() {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    AssetOrderRes res = NetApi.INSTANCE.getAssetOrderList();
                    List<AssetOrder> data = new ArrayList<>();
                    for (AssetOrderRes.DataBean bean:res.getData()) {
                        AssetOrder order = new AssetOrder();
                        order.setCreateTime(bean.getCreateTime());
                        order.setCreateUserName(bean.getCreateUserName());
                        order.setOriginOrderNo(bean.getOriginOrderNo());
                        order.setPickingUserName(bean.getPickingUserName());
                        order.setState(bean.getState());
                        order.setOrderState(bean.getOrderState());
                        order.setId(bean.getId() + "");
                        data.add(order);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onAssetOrderList(data);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getKF(String orderId) {
        LogUtils.v("getKF orderId = " + orderId);
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    WorkAreRes res = NetApi.INSTANCE.getKf(orderId);
                    List<WorkAre> assets = new ArrayList<>();
                    for (WorkAreRes.DataBean bean : res.getData()) {
                        WorkAre asset = new WorkAre();
                        asset.setBarCodePickedCount(bean.getBarCodePickedCount());
                        asset.setBarCodePickingCount(bean.getBarCodePickingCount());
                        asset.setFromWorkAreaId(bean.getFromWorkAreaId());
                        asset.setBarCodeTotalCount(bean.getBarCodeTotalCount());
                        asset.setFromWorkAreaName(bean.getFromWorkAreaName());
                        assets.add(asset);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onWorkAres(assets);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getKW(String OutOrderId,String fromWorkAreaId) {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    KFKWRes res = NetApi.INSTANCE.getKw(OutOrderId,fromWorkAreaId);

                    List<KW> kws = new ArrayList<>();
                    for (KFKWRes.DataBean bean : res.getData()) {
                        String fromLocationId = bean.getFromLocationId();
                        KWAsset KWAsset = new KWAsset();
                        KWAsset.setDisplayName(bean.getDisplayName());
                        KWAsset.setFromContainerId(bean.getFromContainerId());
                        KWAsset.setFromWorkAreaId(bean.getFromWorkAreaId());
                        KWAsset.setState(bean.getState());
                        KWAsset.setMaterialName(bean.getMaterialName());
                        KWAsset.setPlanCount(bean.getPlanCount());
                        KWAsset.setRealCount(bean.getRealCount());
                        KWAsset.setSkuModel(bean.getSkuModel());
                        boolean add = false;
                        for (KW kw : kws) {
                            if (kw.getKw().equals(fromLocationId)) {
                                kw.getAssets().add(KWAsset);
                                add = true;
                                break;
                            }
                        }
                        if (!add) {
                            KW kw = new KW();
                            kw.setKw(fromLocationId);
                            List<KWAsset> kwAssets = new ArrayList<>();
                            kwAssets.add(KWAsset);
                            kw.setAssets(kwAssets);
                            kws.add(kw);
                        }
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onKWList(kws);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected boolean onRfid(String epc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mView!= null) {
                    mView.onBarcode(epc,epc);
                }
            }
        });
        return false;
    }
}
